package top.yekongle.shop.dao.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import top.yekongle.shop.dao.RedisRepository;
import top.yekongle.shop.entity.SysUser;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Map;

/**
 * @author Hank Lau
 * @date 2022/5/4
 */
@Repository
public class RedisRepositoryImpl implements RedisRepository {
	private static final String KEY = "Users";
	private HashOperations<String, Integer, SysUser> hashOperations;

	@Resource
	private RedisTemplate redisTemplate;

	@PostConstruct
	private void init(){
		hashOperations = redisTemplate.opsForHash();
	}

	@Override
	public SysUser find(Integer id) {
		return (SysUser) hashOperations.get(KEY, id);
	}

	@Override
	public Map<Integer, SysUser> findAll() {
		return hashOperations.entries(KEY);
	}

	@Override
	public void save(SysUser user) {
		hashOperations.put(KEY, user.getId(), user);
	}

	@Override
	public void update(SysUser user) {
		hashOperations.put(KEY, user.getId(), user);
	}

	@Override
	public void delete(Integer id) {
		hashOperations.delete(KEY, id);
	}
}
